package com.ccjq.shosestore.web.controller.order;

import com.ccjq.shosestore.core.utils.PageBean;
import com.ccjq.shosestore.core.utils.PageHelper;
import com.ccjq.shosestore.core.utils.UuidUtils;
import com.ccjq.shosestore.member.bo.User;
import com.ccjq.shosestore.order.bo.orderBo;
import com.ccjq.shosestore.order.bo.orderBoGl;
import com.ccjq.shosestore.order.service.impl.orederImpl;
import com.ccjq.shosestore.order.service.orderService;
import com.ccjq.shosestore.shoppingcart.bo.ShopcartBo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "OrderServlet")
public class OrderServlet extends HttpServlet {
    orderService ordao = new orederImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        orderBo or;
        String action = request.getParameter("action");
        User user = (User) request.getSession().getAttribute("user");
        List<ShopcartBo> list = (List<ShopcartBo>) request.getSession().getAttribute("gouwuche");
        boolean b = true;
        if (action.equals("jiesuan")) {
                String orid = UuidUtils.generateShortUUID("U", 10);
                String receiver = request.getParameter("receiver");//姓名
                String province = request.getParameter("province");//省份
                int payment = Integer.parseInt(request.getParameter("payment"));//支付方式
                String city = request.getParameter("city");//城市
                String county = request.getParameter("county");//区县
                String address = request.getParameter("address");//地址
                String zipcode = request.getParameter("zipcode");//邮编
                String telphone = request.getParameter("telphone");//电话
                int amount = Integer.parseInt(request.getParameter("amount"));//总计
                or = new orderBo();
                or.setOrder_id(orid);
                or.setReceiver(receiver);
                or.setProvince(province);
                or.setCity(city);
                or.setCounty(county);
                or.setAddress(address);
                or.setZipcode(zipcode);
                or.setTelphone(telphone);
                or.setPayment(payment);
                or.setAmount(amount);
                or.setUser_id(user.getUserid());
                for (ShopcartBo bo : list) {
                    String prid = bo.getProduct_id();
                    int newQuantity = bo.getQuantity();
                    b = ordao.selectquntity(prid, newQuantity);
                }
                if (b) {
                    ordao.insertOrderXinXi(or);
                    ordao.insertOrderGL(or);
                    ordao.UpdateStock(or);
                    ordao.deleteShopCar(user.getUserid());
                    int zhs = ordao.zhs(user.getUserid());
                    PageBean p = PageHelper.getPager(request, zhs);
                    List<orderBo> orbo = ordao.selectAll(p.getStartRow(), p.getPageSize());
                    request.getSession().setAttribute("orbo", orbo);
                    /*request.getSession().setAttribute("pageBean",p);*/
                    request.getRequestDispatcher("WEB-INF/jsp/order/order_list.jsp").forward(request, response);
                }
               else
                {
                    System.out.print("321333333333333333333333");
                }

        }

        if (action.equals("fenye2")) {
            int zhs = ordao.zhs(user.getUserid());
            PageBean p = PageHelper.getPager(request, zhs);
            List<orderBo> orbo = ordao.selectAll(p.getStartRow(), p.getPageSize());
            request.getSession().setAttribute("orbo", orbo);
            request.getSession().setAttribute("pageBean", p);
            request.getRequestDispatcher("WEB-INF/jsp/order/order_list.jsp").forward(request, response);
        }
        if (action.equals("xq")) {
            String orid = request.getParameter("orid");
            System.out.print("_______________"+orid);
            orderBo oo = ordao.dingdanXinXi(orid);
            List<orderBoGl> or_list = ordao.shangpin(orid);
            int a = 0;
            for (orderBoGl bb : or_list) {
                a += bb.getXiaoji();
            }
            request.getSession().setAttribute("orderBo", oo);
            request.setAttribute("or_list", or_list);
            request.setAttribute("zong", a);
            request.getRequestDispatcher("WEB-INF/jsp/order/order_detail.jsp").forward(request, response);
        }
        if (action.equals("dingdan")) {
            User user1 = (User) request.getSession().getAttribute("user");
            if (user1!=null) {
                int zhs = ordao.zhs(user.getUserid());
                PageBean p = PageHelper.getPager(request, zhs);
                List<orderBo> orbo = ordao.selectAll(p.getStartRow(), p.getPageSize());
                request.getSession().setAttribute("orbo", orbo);
                request.getRequestDispatcher("WEB-INF/jsp/order/order_list.jsp").forward(request, response);
            } else {
                request.getRequestDispatcher("WEB-INF/jsp/member/login.jsp").forward(request, response);
            }

        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
